package org.jeecg.modules.user.customer.service.impl;


import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import org.jeecg.common.api.MemberAPI;
import org.jeecg.common.api.dto.MemberDTO;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.system.util.JwtUtil;
import org.jeecg.modules.constant.BikeConstant;
import org.jeecg.modules.system.announcement.service.IBikeAnnouncementService;
import org.jeecg.modules.system.problem.entity.BikeProblem;
import org.jeecg.modules.system.problem.entity.BikeProblemAppeal;
import org.jeecg.modules.system.problem.mapper.BikeProblemAppealMapper;
import org.jeecg.modules.system.problem.mapper.BikeProblemMapper;

import org.jeecg.modules.user.customer.service.ProblemService;
import org.jeecg.modules.utils.BikeResult;
import org.jeecg.modules.utils.RandomCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Description: 常见问题管理
 * @Author: jeecg-boot
 * @Date:   2021-07-01
 * @Version: V1.0
 */
@Service
public class ProblemServiceImpl extends ServiceImpl<BikeProblemMapper, BikeProblem> implements ProblemService {

    @Autowired
    private BikeProblemMapper bikeProblemMapper;

    @Autowired
    private BikeProblemAppealMapper bikeProblemAppealMapper;

    @Autowired
    private MemberAPI memberAPI;

    @Autowired
    private IBikeAnnouncementService bikeAnnouncementService;
    @Override
    public BikeResult getProblemList(String id,Integer pageNum,Integer pageSize) {

        if (pageNum==null&&pageSize==null){
            pageNum=1;
            pageSize=1;
        }
        Page<BikeProblem> page = new Page<>(pageNum,pageSize);

        QueryWrapper qw =new QueryWrapper();
        if (!StringUtils.isEmpty(id)){
            qw.eq("id",id);
        }
        qw.eq("status","1");
        IPage<BikeProblem> pageList = bikeProblemMapper.selectPage(page, qw);

        return BikeResult.ok("1","查询成功",pageList);
    }

    @Override
    public BikeResult submitAppeal(BikeProblemAppeal bikeProblemAppeal, HttpServletRequest request) {
        String token = request.getHeader(CommonConstant.X_ACCESS_TOKEN);
        String username = JwtUtil.getUsername(token);
        MemberDTO member = memberAPI.getMember(username);
        String uuid = RandomUtil.randomString(16);
        bikeProblemAppeal.setId(uuid);
        bikeProblemAppeal.setCode(RandomCodeUtils.getRandomCode("sq"));
        bikeProblemAppeal.setUserId(member.getId());
        bikeProblemAppeal.setProcessStatus("1");
        int insert = bikeProblemAppealMapper.insert(bikeProblemAppeal);
        //List<String> sysUserIds = bikeAnnouncementService.getSysUserIds(BikeConstant.sys_msg_appeal);
        //bikeAnnouncementService.addSysMsgByUser(sysUserIds,BikeConstant.sys_msg_opinion_text,member.getCode(),BikeConstant.sys_msg_appeal,uuid);
        return BikeResult.ok("1","提交成功",insert);
    }

    @Override
    public BikeResult getAppealList(String id, String processStatus, Integer pageNum, Integer pageSize,HttpServletRequest request) {
        String token = request.getHeader(CommonConstant.X_ACCESS_TOKEN);
        String username = JwtUtil.getUsername(token);
        MemberDTO member = memberAPI.getMember(username);

        if (pageNum==null&&pageSize==null){
            pageNum=1;
            pageSize=1;
        }


        Page<BikeProblemAppeal> page = new Page<>(pageNum,pageSize);
        QueryWrapper qw = new QueryWrapper();
        if (!StringUtils.isEmpty(id)){
            qw.eq("id",id);
        }
        qw.eq("user_id",member.getId());
        if (processStatus.equals("2")){
            qw.eq("process_status","2");
        }else {
            qw.ne("process_status","2");
        }
        IPage<BikeProblemAppeal> page1 = bikeProblemAppealMapper.selectPage(page, qw);

        return BikeResult.ok("1","获取我的诉求成功！",page1);
    }
}
